package com.software.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.software.dao.impl.MessageDetailDaoImpl;
import com.software.model.DrmAgentAllocateReal;
import com.software.model.DrmCourse;
import com.software.model.DrmUser;
import com.software.model.PageListModel;
import com.software.model.PageModel;
import com.software.service.StuMngService;
import com.software.utils.Constants;
import com.software.utils.DateUtils;
import com.software.utils.MyBatisUtils;
import com.software.utils.Saltcheckutil;

@Controller("StudentMngController")
@RequestMapping(value = "/stuControl/*")
public class StudentMngController extends BaseController {

	@Autowired
	private StuMngService stuMngService;
	
	@Autowired
	private MessageDetailDaoImpl mongo;
	
	
	/**
	 * 学员管理 list 查询
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "stuMngList" ,produces = "text/html;charset=UTF-8")
	public ModelAndView stuMngList(@RequestBody PageModel vo, HttpServletResponse res, HttpServletRequest req){
		
		ModelAndView view = new ModelAndView();
		
		vo.setIds(super.getUserIdInSession(req ).toString());
		// 默认页面翻页数据加载
		if(0 == vo.getPageNo() || 0 == vo.getPageSize()) {
			 vo.setPageNow(1);
			 vo.setPageNo(1);
			 vo.setPageSize(6);
		}
		
		//TODO 查询数据库  返回数据 
		if(null != vo.getCreateTimeBegin() && !"".equals(vo.getCreateTimeBegin())) {
			vo.setCreateTimeBegin(vo.getCreateTimeBegin() + " 00:00:00");
		}
		if(null != vo.getCreateTimeEnd() && !"".equals(vo.getCreateTimeEnd())) {
			vo.setCreateTimeEnd(vo.getCreateTimeEnd() + " 24:00:00");
		}
		PageListModel val = stuMngService.mainList(vo);
		
		view.addObject("stulist", val.getShowData());
		
		view.addObject("total", val.getPage().getTotalNum());
		view.addObject("pageSize",val.getPage().getPageSize());
		view.addObject("pageNo",val.getPage().getPageNow());
		
		view.addObject("stuCnt", val.getStuCnt());
		view.addObject("coursTypeCnt", val.getCoursTypeCnt());
		
		view.setViewName("agent/stu");
		
		return	view;
	}

	
	
	/**
	 * 学员 添加init
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "stuAddInit")
	public String stuAddInit(HttpServletResponse res, HttpServletRequest req){
		return	"agent/addstu";
	}
	
	
	/**
	 * 添加学员
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addStu" ,produces = "text/html;charset=UTF-8")
	public String addStu(@RequestBody DrmUser user,HttpServletResponse res, HttpServletRequest req){
		
		//第一 生成salt
		String salt = DateUtils.getCurrentStringDateYYMDHMSS();
		
		//第二 通过密码  和 salt生成新的 加密密码
		String userpass = Saltcheckutil.saltArithmetic(user.getUserPass() , salt);
		
		//将salt  新的passowrod保存数据库
		user.setUserPass(userpass);
		user.setUserSalt(salt);
		user.setUserAcitve(1);		
		stuMngService.creatStu(user,super.getUserIdInSession(req));
		return	"agent/addstu";
	}
	
	
	/**
	 * 分配课程 init
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "stuCourInit")
	public ModelAndView stuCourInit(HttpServletResponse res, HttpServletRequest req){
		DrmUser user = new DrmUser();
		user.setUserTp(Constants.USER_TP_2);
		user.setUserSourceid(super.getUserIdInSession(req));
		
		ModelAndView view = new ModelAndView();
		
		List<DrmCourse> courseList = stuMngService.searchCourseList(user);
		
		view.addObject("courseList", courseList);
		
		view.setViewName("agent/addcour");
		
		return	view;
	}
	
	
	/**
	 * popup选择用户 init
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "popStuInit")
	public ModelAndView searchStuPopUpInit(HttpServletResponse res, HttpServletRequest req){
		
		DrmUser user = new DrmUser();
		user.setUserTp(Constants.USER_TP_0);
		user.setUserSourceid(super.getUserIdInSession(req ));
		//user.setUserRnm(userRnm);
		ModelAndView view = new ModelAndView();
		
		List<DrmUser> list = stuMngService.searchUserList(user);
		
		if(list.size() > 0) {
			view.addObject("userList", list);
			view.addObject("userSize", list.size()/5);
		}
		view.setViewName("agent/popStu");
		
		return	view;
	}
	
	/**
	 * popup选择用户 点击搜索
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "popStuInit2")
	public ModelAndView searchStuPopUpInit2(String name, HttpServletResponse res, HttpServletRequest req){
		
		DrmUser user = new DrmUser();
		user.setUserTp(Constants.USER_TP_0);
		user.setUserSourceid(super.getUserIdInSession(req ));
		user.setUserRnm(name);
		ModelAndView view = new ModelAndView();
		
		List<DrmUser> list = stuMngService.searchUserList(user);
		
		if(list.size() > 0) {
			
			view.addObject("userList", list);
			
			view.addObject("userSize", list.size()/5);
		}
		
		view.setViewName("agent/popStu2");
		
		return	view;
	}
	
	/**
	 * pop选择课程 init
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "popCourInit")
	public ModelAndView popUpCourInit(HttpServletResponse res, HttpServletRequest req){
		
		DrmUser user = new DrmUser();
		user.setUserTp(Constants.USER_TP_2);
		user.setUserSourceid(super.getUserIdInSession(req ));
		
		ModelAndView view = new ModelAndView();
		
		List<DrmCourse> courseList = stuMngService.searchCourseList(user);
		
		view.addObject("courseList", courseList);
		
		view.setViewName("agent/popCour");
		
		return	view;
	}
	
	
	/**
	 * popup选择用户 点击搜索
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "popCourInit2" ,produces = "text/html;charset=UTF-8")
	public ModelAndView popUpCourInit2(@RequestBody DrmUser user, HttpServletResponse res, HttpServletRequest req){
		
		user.setUserTp(Constants.USER_TP_2);
		user.setUserSourceid(super.getUserIdInSession(req ));
		if("全部".equals(user.getCourseTp())) {
			user.setCourseTp("");
		}
		
		ModelAndView view = new ModelAndView();
		
		List<DrmCourse> courseList = stuMngService.searchCourseList(user);
		
		view.addObject("courseList", courseList);
		
		view.setViewName("agent/popCour2");
		
		return	view;
	}
	
	/** 
	 * 保存代理商 学员 课程 关系
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "save" ,produces = "text/html;charset=UTF-8")
	public String saveThird(@RequestBody HashMap<String,Object> map, HttpServletResponse res, HttpServletRequest req){
		if(null == map || null == map.get("userIds")) {
			return	"请选择数据";
		} else {
			map.put("agentid",super.getUserIdInSession(req ));
			stuMngService.save(map);
			return	"ok";
		}

	}
	
	
	
	
}